﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 利用depth在top相机前面显示，通过脚本控制firstview的相机enable来切换视角
/// </summary>
public class FirstView : MonoBehaviour {

    //鼠标方向灵敏度  
    [SerializeField]
    private float sensitivityX = 10F;
    [SerializeField]
    private float sensitivityY = 10F;
    //移动方向灵敏度
    [SerializeField]
    private float moveSensitivity = 10f;

    //上下最大视角(Y视角)  
    [SerializeField,Range(-90f, 0f)]
    private float minimumY = -60F;
    [SerializeField, Range(0f, 90f)]
    private float maximumY = 60F;

    //6个按钮对应6个移动方向，左，后退，右，前，上，下
    private KeyCode[] swc = {KeyCode.A,KeyCode.S,KeyCode.D,KeyCode.W,KeyCode.Q,KeyCode.E };
    private Vector3[] dir = new Vector3[] { };

    //摄像机
    [SerializeField]
    private Camera cm;


    float rotationY = 0F;
    public float SensitivityX
    {
        get
        {
            return sensitivityX;
        }

        set
        {
            if (value==0)
            {
                sensitivityX = 10f;

            }
            else
            {
                sensitivityX = value;
            }
        }
    }

    public float SensitivityY
    {
        get
        {
            return sensitivityY;
        }

        set
        {
            if (value == 0)
            {
                sensitivityY = 10f;

            }
            else
            {
                sensitivityY = value;
            }
        }
    }

    public float MinimumY
    {
        get
        {
            return minimumY;
        }

        set
        {
            minimumY = Mathf.Clamp(value,-90f,90f);
        }
    }

    public float MaximumY
    {
        get
        {
            return maximumY;
        }

        set
        {
            maximumY = Mathf.Clamp(value, -90f, 90f);
        }
    }

    public float MoveSensitivity
    {
        get
        {
            return moveSensitivity;
        }

        set
        {
            moveSensitivity = value;
        }
    }

    private void Awake()
    {
        cm = GetComponentInChildren<Camera>();
       // Probe.FindComponet<Camera>(this,ref cm, "firstView can't find camera", true);//获取相机
       // cm.depth = 1;
        //print(cm == null);
       // cm = GetComponentInChildren<Camera>();
        //MonoBehaviour.print((cm == null) + "2"+"hash:"+cm.GetHashCode()+cm.ToString());

    }
    //使用fixedupdata进行仿真在时间上更精准。因为fixedupdata是固定时间执行的
    void FixedUpdate()
    {
        if (Input.GetMouseButton(0))
        {

            //根据鼠标移动的快慢(增量), 获得相机左右旋转的角度(处理X)  
            float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * SensitivityX;

            //根据鼠标移动的快慢(增量), 获得相机上下旋转的角度(处理Y)  
            rotationY += Input.GetAxis("Mouse Y") * SensitivityY;
            //角度限制. rotationY小于min,返回min. 大于max,返回max. 否则返回value   
            rotationY = Mathf.Clamp(rotationY, MinimumY, MaximumY);

            //总体设置一下相机角度  
            transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);

        }

        //更新方向
        dir = new Vector3[] { -transform.right, -transform.forward, transform.right, transform.forward, transform.up, -transform.up };
        //判断输入 和 进行移动
        for (int i = 0; i < 6; i++)
        {
            if (Input.GetKey(swc[i]))
            {
                transform.position += dir[i] * moveSensitivity * Time.deltaTime;
            }
        }


        //按住左shift加速
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            moveSensitivity *= 2f;
        }
        if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            moveSensitivity /= 2f;
        }

        //第一人称视角开关
        if (Input.GetKeyUp(KeyCode.V))
        {
            if (cm.enabled==true)
            {
                cm.enabled = false;
            }
            else
            {
                cm.enabled = true;
            }
        }
    }


}
